import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import org.powerbot.core.bot.Bot;
import org.powerbot.core.event.listeners.PaintListener;
import org.powerbot.core.script.ActiveScript;
import org.powerbot.core.script.job.Task;
import org.powerbot.core.script.job.state.Node;
import org.powerbot.core.script.job.state.Tree;
import org.powerbot.game.api.Manifest;
import org.powerbot.game.api.methods.Game;
import org.powerbot.game.api.methods.Settings;
import org.powerbot.game.api.methods.Widgets;
import org.powerbot.game.api.methods.input.Keyboard;
import org.powerbot.game.api.methods.interactive.Players;
import org.powerbot.game.api.util.Random;
import org.powerbot.game.api.util.Timer;
import org.powerbot.game.api.wrappers.Tile;
import org.powerbot.game.api.wrappers.interactive.NPC;
import org.powerbot.game.api.wrappers.interactive.Player;
import org.powerbot.game.api.wrappers.widget.WidgetChild;


@Manifest(authors = { "roidskis" }, name = "FireKiller", version = 1.0)
public class FireGiant extends ActiveScript implements PaintListener{
	
	//Script Variables
	public static String user = "l1ck a d1ck",
				pass = "ploplop0";

	public static int[] lootTable = 
	{
		454,18778,1147,1163,1185,1319,1373,1215, 1216, 
		892, 533, 2999, 258, 3001,270,450,
		7937,1443,372,384,1615,1631,1392,
		574,570,2364,452,2362,5315,5316,5289,
		5304, 5300, 1516, 9342, 20667, 6686, 12158
	};

	public static Tile fireGiantLocation = new Tile(3230, 5496, 0);
	public static int[] fireGiants = {1586, 1585, 110};
	
	//other Variables
	private Tree container = null;
	private List<Node> jobs = new ArrayList<Node>();
	public Timer runTime;
	public static List<MyTimer> timers = new ArrayList<MyTimer>();
	public static Timer globalTimer, oneTimer, twoTimer, threeTimer, fourTimer;

	public static NPC currNpc = null,nextNpc = null;
	public static int atkStart = 0, hpStart = 0;
	

	//todo
	// Need to find a priority fire giant , then when killing a giant find a second one
	
	@Override
	public int loop() {
		if(!Game.isLoggedIn())
		{
			doLogin(Game.getClientState());
		}
		if(atkStart == 0)
			atkStart = Settings.get(93);
		if(container !=null) 
        {
			final Node job = container.state();
			if(job != null)
			{
				container.set(job);
				getContainer().submit(job);
				job.join();
			}
        }
        else 
        {
        	provide( new Loot(), new Combat(), new EOC());
        }
       
        return Random.nextInt(100, 300);
	}
	
	public final void provide(final Node... j) 
	{
		for (final Node job : j) 
		{
			if(!jobs.contains(job)) 
			{
				jobs.add(job);
			}
		}
		container = new Tree(jobs.toArray(new Node[jobs.size()]));
	}
	
	public void onStart()
	{
		//ActiveScript.this.
		System.out.println("start");
		runTime = new Timer(0);

		timers.add(new MyTimer(2));//global
		timers.add(new MyTimer(3));//1
		timers.add(new MyTimer(5));//2
		timers.add(new MyTimer(30));//3
		timers.add(new MyTimer(60));//4

		//for(int x=0;x<5;x++)
		//{
			//timers.add(new MyTimer());
		//}
	}
	
	public void onStop()
	{
		runTime = null;
		container = null;
		timers.clear();
		System.out.println("stop");
	}
	
	public void onRepaint(Graphics arg0) {
		Graphics2D g = (Graphics2D) arg0;
		g.setColor(Color.cyan);
		
		if(currNpc!=null)
			for(Polygon p : currNpc.getBounds())
				g.drawPolygon(p);
		
		Player p = Players.getLocal();
		g.drawString("hp percent:"+getHpPercent(), 5, 280);
		g.drawString("Attack Exp : " + ((Settings.get(93) - atkStart)/10), 5, 290);
		if(runTime!=null)
		{
			g.drawString("Time Running:"+ runTime.toElapsedString(), 5, 300);
			g.drawString("Atkxp Hour:"+((3600000 / runTime.getElapsed())*((Settings.get(93) - atkStart)/10)), 5, 310);
		}
		
	}
	
	public static int getHpPercent() {
		int currHP = Widgets.get(748, 5).getHeight();
		return Math.abs(100 - (100 * (currHP - 3) / 31));
	}
	
	public void doLogin(int state)
	{
		switch(state)
		{
		case Game.INDEX_LOGGING_IN:
			System.out.println("Logging in...");
			break;
		case Game.INDEX_LOGIN_SCREEN:
		{
			WidgetChild 
					userField = Widgets.get(596).getChild(39),
					passField = Widgets.get(596).getChild(41);
			
			System.out.println("Logging into +"+user+".");
			
			userField.click(true);
			if(!userField.getText().toLowerCase().equals(user))
			{
				if(userField.getText().length() != 0)
					for(int x=0;x<=userField.getText().length();x++)
						Keyboard.sendKey((char)KeyEvent.VK_BACK_SPACE);
				else
					Keyboard.sendText(user, false);
			}
			
			passField.click(true);
			if(passField.getText().length() != 0)
				for(int x=0;x<=userField.getText().length();x++)
					Keyboard.sendKey((char)KeyEvent.VK_BACK_SPACE);
			else
				Keyboard.sendText(pass, true);
			userField = null;
			passField = null;
			break;
		}
		case Game.INDEX_LOBBY_SCREEN:
			System.out.println("Lobby");
			Widgets.get(906).getChild(186).click(true);
			break;
		case 101://already logged in
			
			break;
		case 102://invalid details
			
			break;
		}
		
	}
}
